package light.head.dao.el;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;

import light.head.cache.Cache;
import light.head.constant.Parm;
import light.head.dao.sys.OrganDAO;
import light.head.enm.RESULT;
import light.head.framework.base.BaseDAO;
import light.head.framework.entity.PageBean;
import light.head.model.el.Course;
import light.head.model.el.CourseLecturer;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
@Service
public class CourseDAO extends BaseDAO<Course, Integer>{
	private OrganDAO organDAO;
	private final String sql_loadAll = "FROM Course c WHERE c.deleted = 0";
	private final String hql_findCousrByCode = "FROM Course c WHERE c.deleted = 0 AND c.code IN ((SELECT tcr.cc FROM TopicCourseRelation tcr WHERE tcr.tc = ?))";
	
	public RESULT findByMap(Integer oc, Map<String, String> map, PageBean pageBean) {
		if(oc == null) return null;
		StringBuffer sql = new StringBuffer("FROM Course c WHERE c.oc = ?");
		List<Object> list = new ArrayList<Object>();
		list.add(oc);
		if(map != null){
			String name = map.get(Parm.NAME);
			String deleted = map.get(Parm.DELETED);
			
			if(isNotNull(name)){
				sql.append(" AND c.name LIKE ?");
				list.add("%" + name + "%");
			}
			if(isNotNull(deleted)){
				sql.append(" AND c.deleted = ?");
				list.add(Boolean.parseBoolean(deleted));
			}
		}
		sql.append(" ORDER BY c.code DESC");
		return findToPageBean(sql.toString(), pageBean, list);
	}
	
	public List<Course> getAll(){
		if (null == Cache.courseList || Cache.courseList.size() == 0){
			loadAll();
		}
		return Cache.courseList;
	}
	
	public List<Course> loadAll(){
		Cache.courseList = find(sql_loadAll);
		return Cache.courseList;
	}
	public RESULT update(Course course){
		if(course == null || course.getCode() == null) return RESULT.ERROR;
		
		Course old = get(course.getCode());
		
		if(old == null) return RESULT.NOT_FIND;
		
		if(isNotNull(course.getName()) && !course.getName().equals(course.getName())){
			old.setName(course.getName());
		}
		
		return upd(old);
	}
	/**
	 * 根据传入的code 获取已经选择的课程信息
	 * @param codes
	 * @return
	 */
	public List<Course> findCourseByCode(Integer code){
		if(code != null){
			return find(hql_findCousrByCode, code);
		}
		return null;
	}
	/**
	 * 辅助查询
	 * @param oc
	 * @param map
	 * @param pageBean
	 * @return
	 */
	public RESULT aidFindByMap(Integer oc, Map<String, String> map, PageBean pageBean){
		if(oc == null) return null;
		List<Object> list = new ArrayList<Object>();
		StringBuffer sql = new StringBuffer("FROM Course c WHERE c.oc = ?");
		list.add(oc);
		if(map != null){
			String name = map.get(Parm.NAME);
			String codes = map.get(Parm.CODES);
			String cn = map.get(Parm.CN);
			
			if(isNotNull(name)){
				sql.append(" AND c.name LIKE ?");
				list.add("%" + name + "%");
			}
			
			if(isNotNull(cn)){
				sql.append(" AND c.name LIKE ?");
				list.add("%" + cn + "%");
			}
			
			if(isNotNull(codes) && codes.length() > 0){
				sql.append(" AND c.code NOT IN (");
				List<Integer> codess = new ArrayList<Integer>();
				String[] orgs = codes.split(",");
				for(int i = 0; i < orgs.length; i++){
					codess.add(Integer.parseInt(orgs[i]));
				}
				for(Integer code : codess){
					sql.append("?,");
					list.add(code);
				}
				sql.deleteCharAt(sql.length() - 1).append(")");
			}
		}
		sql.append(" ORDER BY c.code DESC");
		return findToPageBean(sql.toString(), pageBean, list);
	}
	
	/**
	 * 根据传入的codes 获取已经选择的课程信息
	 * @param codes
	 * @return
	 */
	public List<Course> findCourseByCodes(String codes){
		if(codes != null){
			StringBuffer sb = new StringBuffer("FROM Course c WHERE c.code IN ( ");
			List<Integer> codess = new ArrayList<Integer>();
			String[] orgs = codes.split(",");
			for(int i = 0; i < orgs.length; i++){
				codess.add(Integer.parseInt(orgs[i]));
			}
			for(Integer code : codess){
				sb.append(code + ",");
			}
			sb.deleteCharAt(sb.length() - 1).append(")");
			final String hql = sb.toString();
			return find(hql);
		}
		return null;
	}
	
	
	public List<Course> findAllCourse(){
		String hql = "FROM Course WHERE deleted = 0";
		return find(hql);
	}
	public List<Course> findname(Integer oc, String name){
		String hql = "FROM Course WHERE deleted = 0 AND oc = ? AND name = ?";
		return find(hql,oc,name);
	}
	
	public List<CourseLecturer> findTc(String oc,String cc){
		if(oc == null || cc == null) return null;
		Integer oc1 = organDAO.getRoot(Integer.parseInt(oc)).getCode();
		String hql = "FROM CourseLecturer WHERE oc = ? AND cc = ?";
		return findI(hql,oc1,Integer.parseInt(cc));
	}
	
	
	public List<CourseLecturer> findTc(Integer oc,Integer cc){
		if(oc == null || cc == null) return null;
		String hql = "FROM CourseLecturer WHERE oc = ? AND cc = ?";
		return findI(hql,oc,cc);
	}
	public Course findCc(Integer oc,Integer cc){
		if(oc == null || cc == null) return null;
		String hql = "FROM Course WHERE oc = ? AND code = ?";
		return (Course) get(hql,oc,cc);
	}
	
	@Autowired
	public void setOrganDAO(OrganDAO organDAO) {
		this.organDAO = organDAO;
	}
	
	
}
